import { MedicineItem } from '@/interfaces/medicine'
import type { ProColumns } from '@ant-design/pro-components';
import { Button } from 'antd'
import showDeleteConfirm from '@/utils/deleteConfirm';
import { deleteMedicineOne } from '@/services/medicine'
import WithAuthComp from '@/components/WithAuthComp';
import { ROLE } from '@/common/const'

const getColumns: (dialogRef, setItem, actionRef) => ProColumns<MedicineItem>[] = (dialogRef, setItem, actionRef) => {
    return [
        {
            title: '药品名称',
            dataIndex: 'name',
            hideInSearch: true
        },
        {
            title: '生产厂家',
            dataIndex: 'factory',
        },
        {
            title: '规格',
            dataIndex: 'specs',
            hideInSearch: true
        },
        {
            title: '剂型',
            dataIndex: 'type',
            hideInSearch: true
        },
        {
            title: '包装',
            dataIndex: 'pack',
            hideInSearch: true
        },
        {
            title: '单价',
            dataIndex: 'price',
            hideInSearch: true
        },
        {
            title: '库存',
            dataIndex: 'entercount',
            hideInSearch: true
        },
        {
            title: '总金额',
            dataIndex: 'totalprice',
            hideInSearch: true
        },
        {
            title: '生产批号',
            dataIndex: 'batchnumber',
        },
        {
            title: '生产日期',
            dataIndex: 'productdate',
            valueType: 'date',
            sorter: true,
            hideInSearch: true,
        },
        {
            title: '生产日期',
            dataIndex: 'productdate',
            valueType: 'dateRange',
            hideInTable: true,
            search: {
                transform: (value) => {
                    return {
                        productdate_startTime: value[0],
                        productdate_endTime: value[1],
                    };
                },
            },
        },
        {
            title: '有效截止日期',
            dataIndex: 'deaddate',
            valueType: 'date',
            sorter: true,
            hideInSearch: true,
        },
        {
            title: '有效截止日期',
            dataIndex: 'deaddate',
            valueType: 'dateRange',
            hideInTable: true,
            search: {
                transform: (value) => {
                    return {
                        deaddate_startTime: value[0],
                        deaddate_endTime: value[1],
                    };
                },
            },
        },
        {
            title: '操作',
            valueType: 'option',
            key: 'option',
            render: (text, record, _, action) => [
                <Button
                    size='small'
                    type='primary'
                    key={'out'}
                    onClick={() => {
                        setItem({ id: record.id, count: record.entercount })
                        dialogRef?.current?.showModelRef();
                    }}
                >
                    出库
                </Button>,
                <WithAuthComp authKey={[ROLE.ADMIN]}>
                    <Button
                        size='small'
                        type="primary"
                        danger
                        key={'delete'}
                        onClick={() => {
                            showDeleteConfirm({ id: record.id, action, deleteApi: deleteMedicineOne, cb: actionRef?.current.reload })
                        }}
                    >
                        删除
                    </Button>
                </WithAuthComp>
                ,
            ],
        },
    ];
}

export default getColumns;